package edu.spec.xhd.file;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import edu.spec.xhd.R;

public class ActivityFile extends AppCompatActivity {

    private EditText et_write;
    private Button btn_write;
    private Button btn_read;
    private TextView tv_read;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file);
        initViews();
        initListenres();
    }

    private void initViews(){
        et_write = findViewById(R.id.et_write);
        btn_write = findViewById(R.id.btn_write);
        btn_read = findViewById(R.id.btn_read);
        tv_read = findViewById(R.id.tv_read);
    }

    private void initListenres(){
        btn_write.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String str = et_write.getText().toString();
                        FileOutputStream fos = null;
                        BufferedWriter bufferedWriter = null;
                        try {
                            fos = openFileOutput("str", Context.MODE_APPEND);
                            bufferedWriter = new BufferedWriter(new OutputStreamWriter(fos));
                            bufferedWriter.write(str);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                bufferedWriter.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
        );
        btn_read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            FileInputStream fis = null;
            BufferedReader bufferedReader = null;
            StringBuilder builder = new StringBuilder();
            try {
                fis = openFileInput("str");
                bufferedReader = new BufferedReader(new InputStreamReader(fis));
                String line = "";
                while ((line = bufferedReader.readLine())!=null){
                    builder.append(line);
                }
                tv_read.setText(builder.toString());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            }
        });
    }

}
